package org.summer.lawyer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.summer.common.core.exception.ServiceException;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.UserFollow;
import org.summer.lawyer.domain.bo.userfollow.UserFollowBo;
import org.summer.lawyer.domain.vo.userfollow.UserFollowVo;
import org.summer.lawyer.mapper.UserFollowMapper;
import org.summer.lawyer.service.IUserFollowService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * 
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements IUserFollowService {


    @Override
    public TableDataInfo<UserFollowVo> selectListByWxUserId(Long wxUserId) {
        LambdaQueryWrapper<UserFollow> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserFollow::getWxUserId, wxUserId)
                .eq(UserFollow::getCancelFlag,"0")
                .orderByDesc(UserFollow::getFollowTime);

        return TableDataInfo.build(baseMapper.selectVoList(queryWrapper));
    }

    /**
     * 查询用户关注
     *
     * @param followId 主键
     * @return 用户关注
     */
    @Override
    public UserFollowVo selectUserFollowVoById(Long followId){
        return baseMapper.selectVoById(followId);
    }

    @Override
    public UserFollowVo selectUserFollowVoByUserId(Long wxUserId, Long followeeId) {
        LambdaQueryWrapper<UserFollow> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserFollow::getWxUserId, wxUserId)
                .eq(UserFollow::getFolloweeId, followeeId);
        return baseMapper.selectVoOne(queryWrapper,false);
    }

    /**
     * 分页查询用户关注列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户关注分页列表
     */
    @Override
    public TableDataInfo<UserFollowVo> selectPageList(UserFollowBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserFollow> lqw = buildQueryWrapper(bo);
        Page<UserFollowVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户关注列表
     *
     * @param bo 查询条件
     * @return 用户关注列表
     */
    @Override
    public List<UserFollowVo> selectUserFollowVoList(UserFollowBo bo) {
        LambdaQueryWrapper<UserFollow> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserFollow> buildQueryWrapper(UserFollowBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserFollow> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWxUserId() != null, UserFollow::getWxUserId, bo.getWxUserId());
        lqw.eq(bo.getFollowTime() != null, UserFollow::getFollowTime, bo.getFollowTime());
        lqw.eq(bo.getUnfollowTime() != null, UserFollow::getUnfollowTime, bo.getUnfollowTime());
        lqw.eq(StrUtil.isNotBlank(bo.getCancelFlag()), UserFollow::getCancelFlag, bo.getCancelFlag());
        lqw.eq(bo.getFolloweeId() != null, UserFollow::getFolloweeId, bo.getFolloweeId());
        lqw.eq(StrUtil.isNotBlank(bo.getFolloweeScope()), UserFollow::getFolloweeScope, bo.getFolloweeScope());
        lqw.like(StrUtil.isNotBlank(bo.getFolloweeName()), UserFollow::getFolloweeName, bo.getFolloweeName());
        lqw.eq(StrUtil.isNotBlank(bo.getAvatar()), UserFollow::getAvatar, bo.getAvatar());
        lqw.eq(StrUtil.isNotBlank(bo.getFolloweeDesc()), UserFollow::getFolloweeDesc, bo.getFolloweeDesc());
        lqw.like(StrUtil.isNotBlank(bo.getFollowName()), UserFollow::getFollowName, bo.getFollowName());
        lqw.eq(StrUtil.isNotBlank(bo.getFollowAvatar()), UserFollow::getFollowAvatar, bo.getFollowAvatar());
        return lqw;
    }

    /**
     * 新增用户关注
     *
     * @param bo 用户关注
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserFollowBo bo) {
        UserFollow add = MapstructUtils.convert(bo, UserFollow.class);
        validEntityBeforeSave(add);
        add.setFollowTime(DateUtil.date());
        add.setCancelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFollowId(add.getFollowId());
        }
        return flag;
    }

    /**
     * 修改用户关注
     *
     * @param bo 用户关注
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserFollowBo bo) {
        UserFollow update = MapstructUtils.convert(bo, UserFollow.class);
        update.setCancelFlag("0");
        update.setFollowTime(DateUtil.date());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserFollow entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户关注信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean checkUnique(UserFollowBo bo) {
        return baseMapper.exists(new LambdaQueryWrapper<UserFollow>().eq(UserFollow::getWxUserId, bo.getWxUserId()).eq(UserFollow::getFolloweeId,bo.getFolloweeId()).eq(UserFollow::getCancelFlag,"0"));
    }

    @Override
    public void follow(UserFollowBo bo) {
        if(checkUnique(bo)){
            throw new ServiceException("您已经关注过了");
        }
        UserFollowVo userFollowVo = selectUserFollowVoByUserId(bo.getWxUserId(), bo.getFolloweeId());
        if(userFollowVo == null){
            insertByBo(bo);
        }else{
            bo.setFollowId(userFollowVo.getFollowId());
           updateByBo(bo);
        }
    }

    @Override
    public void unfollow(UserFollowBo bo) {
        UserFollowVo userFollowVo = selectUserFollowVoByUserId(bo.getWxUserId(), bo.getFolloweeId());
        if(userFollowVo != null){
            UserFollow userFollow = new UserFollow();
            userFollow.setCancelFlag("1");
            userFollow.setUnfollowTime(DateUtil.date());
            LambdaUpdateWrapper<UserFollow> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.eq(UserFollow::getFollowId,userFollowVo.getFollowId());
            baseMapper.update(userFollow, updateWrapper);
        }
    }
}
